package com.hldsmart.smartda.adapter.home;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hldsmart.smartda.R;
import com.hldsmart.smartda.model.entity.MyQuanEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by liuzhihui on 2017/2/17.
 *
 */

public class MyquanAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private ArrayList<MyQuanEntity> list = new ArrayList<>();
    private MyquanAdapter.OnItemClickedListener listener;

    public void addData(List<MyQuanEntity> data) {
        list.addAll(data);
        notifyDataSetChanged();
    }

    public void clear() {
        list.clear();
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view;
        view = LayoutInflater.from(context).inflate(R.layout.rv_quan_item, parent, false);
        return new MyquanAdapter.ListViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

            MyquanAdapter.ListViewHolder listViewHolder = (MyquanAdapter.ListViewHolder) holder;
            listViewHolder.tv_quan_name.setText(list.get(position).getQuanName()+"店内折扣优惠券");
            listViewHolder.tv_quan_time.setText("有效期至："+list.get(position).getQuanTime());
            listViewHolder.tv_quan_No.setText("码券："+list.get(position).getId());
            listViewHolder.ll_myquan.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onPhotoClicked(list.get(position));
                    }
                }
            });

    }


    @Override
    public int getItemCount() {
        return list.size();
    }


    public void setListener(MyquanAdapter.OnItemClickedListener listener) {
        this.listener = listener;
    }

    public interface OnItemClickedListener {
        void onPhotoClicked(MyQuanEntity myquanEntity);
    }

    class ListViewHolder extends RecyclerView.ViewHolder {


        @Bind(R.id.tv_quan_name)
        TextView tv_quan_name;

        @Bind(R.id.tv_quan_time)
        TextView tv_quan_time;

        @Bind(R.id.tv_quan_No)
        TextView tv_quan_No;
        @Bind(R.id.ll_myquan)
        LinearLayout ll_myquan;


        public ListViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

    }
}